package web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import entity.Command;
import entity.CommandContent;
import enums.travelenums;
import service.Command_service;

@Controller
public class Command_controller {
	
	@Autowired
	private Command_service command_service;
	
	@RequestMapping(value="autoreplay",method=RequestMethod.POST,produces ="text/html;charset=UTF-8")
	@ResponseBody
	public String Autoreplay(@RequestParam("content")String content){
		String description =command_service.getDescription(content);
		if(description.equals(travelenums.NOTFOUND.getInfo())){
			String forcontent=command_service.getContent(content);
			return forcontent;
		}
		return description;
	}
	
	/*后台*/
	
	//命令列表页
	@RequestMapping(value="commandlist",method=RequestMethod.POST)
	public String CommandList(@RequestParam(value="page",defaultValue="1",required=true)Integer page,Model model){
		Page<Command> pa=PageHelper.startPage(page,8);
		List<Command> list=command_service.CommandList();
		PageInfo<Command> pageInfo=new PageInfo<Command>(pa);
		model.addAttribute("page", pageInfo);
		model.addAttribute("CommandList", list);
		return "back/command";
	}
	//命令详情页
	@RequestMapping(value="commanddetail",method=RequestMethod.POST)
	public String CommandDetail(@RequestParam(value="id")Integer id,Model model){
		Command command=command_service.getCommandById(id);
		model.addAttribute("Command", command);
		return "back/editcommand";
	}
	
	@RequestMapping(value="update/commandcontent",method=RequestMethod.POST)
	@ResponseBody
	public String updateCommandContent(CommandContent commandcontent){
		if(commandcontent!=null){
			boolean boo=command_service.updateCommandContent(commandcontent);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
	
	@RequestMapping(value="update/command",method=RequestMethod.POST)
	@ResponseBody
	public String updateCommand(Command command){
		if(command!=null){
			boolean boo=command_service.updateCommand(command);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
	@RequestMapping(value="save/commandcontent",method=RequestMethod.POST)
	@ResponseBody
	public String saveCommandContent(CommandContent commandContent){
		if(commandContent!=null){
			boolean boo=command_service.saveCommandContent(commandContent);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
	
	@RequestMapping(value="save/command",method=RequestMethod.POST)
	@ResponseBody
	public String saveCommand(Command command){
		if(command!=null){
			boolean boo=command_service.saveCommand(command);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
	
	@RequestMapping(value="delete/command",method=RequestMethod.POST)
	@ResponseBody
	public String deleteCommand(Integer id){
		if(id!=null){
			if(command_service.deleteCommand(id)){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
	
	@RequestMapping(value="delete/commandcontent",method=RequestMethod.POST)
	@ResponseBody
	public String deleteCommandContent(Integer id){
		if(id!=null){
			if(command_service.deleteCommandContent(id)){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.ERRO.getInfo();
	}
}
